package com.mystudy.management.usermanagement.service;

import com.mystudy.management.usermanagement.dao.mappers.RequestTraceMapper;
import com.mystudy.management.usermanagement.dao.mappers.VisitTraceMapper;
import com.mystudy.management.usermanagement.dao.pojo.RequestTrace;
import com.mystudy.management.usermanagement.dao.pojo.RequestTraceExample;
import com.mystudy.management.usermanagement.dao.pojo.VisitTrace;
import com.mystudy.management.usermanagement.dao.pojo.VisitTraceExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
//声明事务
@Transactional(
        isolation = Isolation.REPEATABLE_READ//事务隔离级别：可重复读
)
public class Index2_1ServiceImpl implements Index2_1Service {

    @Autowired
    private VisitTraceMapper visitTraceMapper;

    @Autowired
    private RequestTraceMapper requestTraceMapper;


    /*
    ------------------------------------------------------访问记录-----------------------------------------------------------
     */

    //查询所有记录
    @Override
    public List<VisitTrace> selectAllLog() {
        List<VisitTrace> visitTraces ;
        visitTraces  = visitTraceMapper.selectByExample(null);
        if (visitTraces.size() == 0 ) {
            visitTraces = new ArrayList<>();
        }
        return visitTraces;


    }


    //通过用户id查询访问记录
    @Override
    public List<VisitTrace> selectLogByCondition(Integer id) {
        List<VisitTrace> visitTraces ;
        VisitTraceExample example = new VisitTraceExample();
        example.createCriteria().andUserIdEqualTo(id);
        visitTraces  = visitTraceMapper.selectByExample(example);
        if (visitTraces.size() == 0) {
            visitTraces = new ArrayList<>();
        }
        return visitTraces;
    }



    //通过IP查询所有记录
    @Override
    public List<VisitTrace> selectLogByCondition(String IP) {
        List<VisitTrace> visitTraces ;
        VisitTraceExample example = new VisitTraceExample();
        example.createCriteria().andVisitIpNotEqualTo(IP);
        visitTraces  = visitTraceMapper.selectByExample(example);
        if (visitTraces.size() == 0) {
            visitTraces = new ArrayList<>();
        }
        return visitTraces;
    }


    //指定Id某个时间段
    @Override
    public List<VisitTrace> selectLogByCondition(Integer id, Date startDate, Date endDate) {
        List<VisitTrace> visitTraces ;
        VisitTraceExample example = new VisitTraceExample();
        example.createCriteria().andUserIdEqualTo(id).andVisitDateBetween(startDate,endDate);
        visitTraces  = visitTraceMapper.selectByExample(example);
        if (visitTraces.size() == 0 ) {
            visitTraces = new ArrayList<>();
        }
        return visitTraces;
    }

    //指定IP某个时间段
    @Override
    public List<VisitTrace> selectLogByCondition(String IP, Date startDate, Date endDate) {
        List<VisitTrace> visitTraces ;
        VisitTraceExample example = new VisitTraceExample();
        example.createCriteria().andVisitIpEqualTo(IP).andVisitDateBetween(startDate,endDate);
        visitTraces  = visitTraceMapper.selectByExample(example);
        if (visitTraces.size() == 0 ) {
            visitTraces = new ArrayList<>();
        }
        return visitTraces;
    }

    //批量删除访问记录
    @Override
    public Map<String,Integer[]> deleteLogByConditions(Integer[] logIds) {
        Map<String,Integer[]> resultResponse  = new HashMap<>();
        resultResponse.put("Failed",new Integer[]{});
        for(Integer id: logIds){
            if(this.visitTraceMapper.selectByPrimaryKey(id) != null ){
                this.visitTraceMapper.deleteByPrimaryKey(id);
            }else {
                Integer[] failedField = resultResponse.get("Failed");
                Integer[] newFailedField = new Integer[failedField.length+1];
                newFailedField[failedField.length] = id;
                resultResponse.put("Failed",newFailedField);
            }
        }

        return resultResponse;
    }


/*
---------------------------------------------------------请求记录----------------------------------------------------------
 */

    //查所有记录
    @Override
    public List<RequestTrace> selectAllRequestLog() {
        List<RequestTrace> requestTraces ;
        requestTraces  = requestTraceMapper.selectByExample(null);
        if (requestTraces.size() == 0 ) {
            requestTraces = new ArrayList<>();
        }
        return requestTraces;
    }


    //查指定IP
    @Override
    public List<RequestTrace> selectRequestLogByCondition(String IP) {
        List<RequestTrace> requestTraces ;
        RequestTraceExample example = new RequestTraceExample();
        example.createCriteria().andTraceIpEqualTo(IP);
        requestTraces  = requestTraceMapper.selectByExample(example);
        if (requestTraces.size() == 0) {
            requestTraces = new ArrayList<>();
        }
        return requestTraces;
    }


    //有日期和时间限制的查询
    @Override
    public List<RequestTrace> selectRequestLogByCondition(String IP, Date startDate, Date endDate) {
        List<RequestTrace> requestTraces ;
        RequestTraceExample example = new RequestTraceExample();
        example.createCriteria().andTraceIpEqualTo(IP).andTraceDateBetween(startDate,endDate);
        requestTraces  = requestTraceMapper.selectByExample(example);
        if (requestTraces.size() == 0 ) {
            requestTraces = new ArrayList<>();
        }
        return requestTraces;
    }

    //只有时间限制的查询
    @Override
    public List<RequestTrace> selectRequestLogByCondition(Date startDate, Date endDate) {
        List<RequestTrace> requestTraces ;
        RequestTraceExample example = new RequestTraceExample();
        example.createCriteria().andTraceDateBetween(startDate,endDate);
        requestTraces  = requestTraceMapper.selectByExample(example);
        if (requestTraces.size() == 0 ) {
            requestTraces = new ArrayList<>();
        }
        return requestTraces;
    }

    @Override
    public Map<String, Integer[]> deleteRequestLogByConditions(Integer[] traceIds) {
        Map<String,Integer[]> resultResponse  = new HashMap<>();
        resultResponse.put("Failed",new Integer[]{});
        for(Integer id: traceIds){
            if(this.requestTraceMapper.selectByPrimaryKey(id) != null ){
                this.requestTraceMapper.deleteByPrimaryKey(id);
            }else {
                Integer[] failedField = resultResponse.get("Failed");
                Integer[] newFailedField = new Integer[failedField.length+1];
                newFailedField[failedField.length] = id;
                resultResponse.put("Failed",newFailedField);
            }
        }

        return resultResponse;
    }
}
